/*
	Estimates the degree of selection bias into the Glassdoor dataset for
	Chinese college graduates employed in China
	Output:	Figure C1b
			Selection_China.csv
*/

local seed "C:\Users\jsock\Dropbox\Research\GD\International"

local dataPath "`seed'/Data"
local inputPath "`seed'/InputData"
local figurePath "`seed'/Replication/Figures"
local tablePath "`seed'/Replication/Tables"
local estimatePath "`seed'/Replication/Estimates"
local tempPath "`seed'/Replication/TempData"

********************************************************
* Save exchange rates as dta file
********************************************************

clear 
set more off
set matsize 10000

insheet using "`inputPath'/Exchange_rates_2022.csv", comma
 
keep country_glassdoor year ppp_xrat  

save `tempPath'/Exchange_rates.dta, replace

*-------------------------------- 
* Thresholds used for sample selection
*--------------------------------
.
scalar country_premia_thresh = 25

scalar selection_thresh = 25

scalar school_thresh = 25 

local currentCountry = "China"
local saveCountry = "China"

********************************************************
* Read in UK undergraduate earnings
********************************************************

clear 
set more off
set matsize 10000

insheet using "`inputPath'/`saveCountry'_universities_earnings.csv", comma

generate year = 2018

generate country_glassdoor = "`currentCountry'"

merge m:1 country_glassdoor year using `tempPath'/Exchange_rates.dta

keep if _merge == 3
drop _merge

generate undergrad_pay_2013 =  salary_2013 * ppp_xrat * 12
generate undergrad_pay_2015 =  salary_2015 * ppp_xrat * 12
generate undergrad_pay_2017 =  salary_2017 * ppp_xrat * 12

sort school year

collapse (mean) undergrad_pay* , by(school)

replace school = "Sun Yat-Sen University" if school == "Sun Yet-Sen University"
replace school = "China University of Petroleum" if strpos(school,"China University of Petroleum")
replace school = "Shanghai Jiao Tong University" if school == "Shanghai Jiaotong University"

replace school = subinstr(school," And "," and ",.)

replace school = "Nanjing University of Posts And Telecommunications" if school == "Nanjing University of Posts and Telecommunications"
replace school = "East China University of Political Science And Law" if school == "East China University of Political Science and Law"
replace school = "China University of Political Science And Law" if school == "China University of Political Science and Law"
replace school = "China University of Mining And Technology" if school == "China University of Mining and Technology"
replace school = "Beijing Technology And Business University" if school == "Beijing Technology and Business University"

save `tempPath'/`saveCountry'_universities_earnings_merge.dta, replace

********************************************************
* Read in country gdp to get universitycountry gdp
********************************************************

clear 
set more off
set matsize 10000
set scheme s1mono

* Set path and load data 
insheet using "`dataPath'/Salaries_international_dataset_main.csv", comma
drop v1

drop if jobtitle == ""

drop metro shortname 
drop city basecurrency country_iso dateval  
drop gender
drop sectorname iscurrentjobflag 

generate national_rank_pct = national_rank / numberuniversities

*--------------------------------
* Exclude users that leave more than 10 reviews
*--------------------------------

sort fk_userid yearofsalary 

by fk_userid : gen userReviews = _N

drop if userReviews > 10

drop userReviews

*--------------------------------
* Convert with ppp exchange rates
*--------------------------------

generate logbase = ln(basesalary * ppp_xrat)

drop if logbase == .

*--------------------------------
* Exclude outliers in base pay
*--------------------------------

scalar scalingThresh = 10

generate realbase = basesalary * ppp_xrat
generate outside_thresh =  (realbase < (1/scalingThresh) * gdppw) | (realbase > scalingThresh * gdppw) 
drop realbase

drop if outside_thresh

*--------------------------------
* Keep only country
*--------------------------------

drop if employertypecode == "SELF_EMPLOYED" 

keep if countryname == "`currentCountry'"

keep if universitycountry == "`currentCountry'"

*--------------------------------
* Generate indicators for valid in education analysis
*--------------------------------

generate hasDegree = degree != "UNMATCHED" & degree != "missing" & degree != ""  & degree != "HIGHSCHOOL"

generate uniDegree = degree == "BACHELORS" 

generate hasSchool = school != ""

generate valid_educ = uniDegree & hasDegree & hasSchool & universitycountry != ""

keep if valid_educ 

********************************************************
* Create valid sampels for one and two year comparisons
********************************************************

*--------------------------------
* Years since school ended
*--------------------------------

split endschool, p("-")

destring endschool1, gen(end_year)

generate years_from_graduate = yearofsalary - end_year

*-------------------------------- 
* Add comparison earnings
*--------------------------------

merge m:1 school using `tempPath'/`saveCountry'_universities_earnings_merge.dta

keep if _merge == 3
drop _merge

*--------------------------------
* Years since school ended
*--------------------------------

bys school : egen avg_grad_17 = median(logbase) if inrange(years_from_graduate,1,1)

bys school : egen avg_grad_15 = median(logbase) if inrange(years_from_graduate,3,3)

bys school : egen avg_grad_13 = median(logbase) if inrange(years_from_graduate,5,5)

bys school : gen n_grad_17 = sum(inrange(years_from_graduate,1,1))

bys school : gen n_grad_15 = sum(inrange(years_from_graduate,3,3))

bys school : gen n_grad_13 = sum(inrange(years_from_graduate,5,5))

*--------------------------------
* Compare External data and GD data for recent graduates
*--------------------------------

local currentCountry = "China"
local saveCountry = "China"

preserve

	collapse (max) avg_grad_* n_grad_* undergrad_pay* national_rank_pct, by(school)
	
	* Stack earnings for different grad years 
	
	expand 3
	
	sort school

	generate gradYears = .
	by school: replace gradYears = 1 if _n == 1
	by school: replace gradYears = 3 if _n == 2
	by school: replace gradYears = 5 if _n == 3
	
	generate avg_grad = .
	replace avg_grad = avg_grad_17 if gradYears == 1
	replace avg_grad = avg_grad_15 if gradYears == 3
	replace avg_grad = avg_grad_13 if gradYears == 5
	
	generate undergrad_earnings = .
	replace undergrad_earnings = undergrad_pay_2017 if gradYears == 1
	replace undergrad_earnings = undergrad_pay_2015 if gradYears == 3
	replace undergrad_earnings = undergrad_pay_2013 if gradYears == 5
	
	generate n_grad = .
	replace n_grad = n_grad_17 if gradYears == 1
	replace n_grad = n_grad_15 if gradYears == 3
	replace n_grad = n_grad_13 if gradYears == 5
	
	generate avg_median_undergrad = ln(undergrad_earnings)
		
	* Difference between scorecard and gd 
	generate diff_grad = avg_grad - avg_median_undergrad	
	
	egen total_grad = sum(n_grad)
	
	keep if diff_grad != .
	
	rename n_grad n_gd 
	bys school : egen n_grad = sum(n_gd)
	
	collapse (mean) diff_grad (max) n_grad national_rank_pct [aw=n_gd], by(school )
	
	* Get averages
	sum diff_grad
	local avg_equal = r(mean)
	scalar est_wtd_equal = r(mean)
	
	sum diff_grad [aw=n_grad]
	local avg_wtd_gd = r(mean)
	scalar est_wtd_gd = r(mean)
	scalar sum_wt_wtd_gd = r(sum_w)

		* Number of universities
		tab school if diff_grad != .
		scalar n_wt_wtd_gd = r(r)

	* Get averages for top 5%
	sum diff_grad if national_rank_pct <= 0.05 [aw=n_grad]
	local avg_wtd_gd_top5 = r(mean)
	scalar est_wtd_gd_top5 = r(mean)
	scalar sum_wt_wtd_gd_top5 = r(sum_w)
	
		* Number of universities
		tab school if diff_grad != . & national_rank_pct <= 0.05 
		scalar n_wt_wtd_gd_top5 = r(r)
	
	* Get averages for not top 5%
	sum diff_grad if national_rank_pct > 0.05 | national_rank_pct == . [aw=n_grad]
	local avg_wtd_gd_not5 = r(mean)
	scalar est_wtd_gd_not5 = r(mean)
	scalar sum_wt_wtd_gd_not5 = r(sum_w)
	
		* Number of universities
		tab school if diff_grad != . & (national_rank_pct > 0.05 | national_rank_pct == .)
		scalar n_wt_wtd_gd_not5 = r(r)

	* Weighted and unweighted kernel density
	twoway kdensity diff_grad [aw=n_grad], lcolor(gs3) lpattern(dash) xscale(r(-1.25(0.25)1.25)) xlabel(-1.25(0.25)1.25)  ///
		xline(0, lcolor(black) lpattern(solid) lwidth(thin)) fcolor(none) ///
		xtitle("log difference within college") ytitle("density") title("") scale(1.2)
	graph export "`figurePath'/Figure_C1b.eps"	, replace

restore

*--------------------------------
* Export summary of selection estimates
*--------------------------------

preserve

	generate selection_est_equal = est_wtd_equal
	generate selection_est_wtd = est_wtd_gd
	generate selection_est_wtd_top5 = est_wtd_gd_top5
	generate selection_est_wtd_not5 = est_wtd_gd_not5

	generate selection_wtd_N = n_wt_wtd_gd
	generate selection_wtd_N_top5 = n_wt_wtd_gd_top5
	generate selection_wtd_N_not5 = n_wt_wtd_gd_not5

	generate selection_wtd_sum = sum_wt_wtd_gd
	generate selection_wtd_sum_top5 = sum_wt_wtd_gd_top5
	generate selection_wtd_sum_not5 = sum_wt_wtd_gd_not5

	keep universitycountry selection_*
	
	keep if _n == 1
	
	order universitycountry selection_wtd_N* selection_wtd_sum* selection_est_* 
	
	outsheet using "`estimatePath'\Selection_`saveCountry'.csv" , comma replace

restore
